<?php
namespace bxd\lib\validator\rule;

/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package lib
 * @name Email
 * @subpackage check
 */
class NotInDbTable extends \bxd\lib\validator\AbstractCode implements \bxd\lib\validator\InterfaceRule
{
	public function __construct($value = null)
	{
		$this->_value = $value;
	}

	/**
	 * Validazione della regola
	 *
	 * @return boolean
	 */
	public function validate()
	{
		if($this->_value == '' || $this->_value === null)
			return true;
		
		$sql = "SELECT COUNT(*) tot_match
				FROM `" . $this->_params[0] . "`
				WHERE `" . $this->_params[1] . "` = ?";
		
		$otherParam = array();
		
		if(count($this->_params[2]))
		{
			$sqlAdd = array_keys($this->_params[2]);
			$sqlAdd = $sqlAdd[0];
			
			$otherParam = array_values($this->_params[2]);
			$otherParam = $otherParam[0];
			
			if(is_numeric($sqlAdd) && is_string($otherParam))
			{
				$sqlAdd = $otherParam;
				$otherParam = array();
			}
			
			if(is_string($otherParam))
				$otherParam = array();
			
			$sql .= " AND {$sqlAdd}";
		}
		
		$dbprepare = \bxd\lib\db\Db::getInstance()->prepare($sql)->bindParam("?", $this->_value);
		
		foreach($otherParam as $otherValue)
			$dbprepare->bindParam("?", $otherValue);
		
		$result = $dbprepare->execute();
		
		if($result->fetchColumn(0))
			return false;
		
		return true;
	}

	/**
	 * Messaggio di errore
	 *
	 * @return string
	 */
	public function message()
	{
		return sprintf(_("The value '%s' is not valid"), $this->_value);
	}

	/**
	 * Codice dell'errore
	 *
	 * @return string
	 */
	public function code()
	{
		return self::$checkNotInDbTable;
	}
	
	/**
	 * Restitusice la lista degli attributi presenti nel controllo
	 *
	 * @return array
	 */
	public function attribute()
	{
		return array
		(
			"value" => $this->_value,
			"table" => $this->_params[0],
			"field" => $this->_params[1],
			"otherCondition" => $this->_params[2]
		);
	}
}
?>
